<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *贡献值明细
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'all';
$return=array();
$tabs=array(
	array('op'=>'all','title'=>'累计贡献值'),
	array('op'=>'list','title'=>'贡献值记录'),
	//array('op'=>'listday','title'=>'日贡献值统计'),
	//array('op'=>'listmonth','title'=>'月贡献值统计'),
);
if($_RQ['op']=='all'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['mid'])){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
    $orderby='gongfee desc';
	$total='';
	$list=pdo_getslice('jy_merch3fee_member',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$starttime=$day;
	$endtime=$starttime+24*3600;
	foreach($list as &$v){
		$v['member']=cfc('core')->member_show($v['mid']);
		$v['gongfee']=pdo_getsum('jy_merch3fee_commission',array('merchid'=>$_SESSION['onemerchid'],'type'=>7,'uniacid'=>$_SESSION['uniacid'],'mid'=>$v['mid']),'money');
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'贡献值:按1:1自购金额获得贡献值，此贡献值为统计数据，作为他用；','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
				array('name'=>'member','title'=>'会员信息_Member','type'=>'member'),
				array('name'=>'gongfee','title'=>'累计贡献值','type'=>'unitstring','unit'=>'分'),
				array('name'=>'createtime','title'=>'入驻时间','type'=>'time'),
		)),
		'search'=>array(
			array('name'=>'mid','width'=>15,'title'=>'会员id','type'=>'choose','action'=>'core/member.get_member/'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
//贡献值明细
if($_RQ['op']=='list'){
	$params=array('merchid'=>$_SESSION['onemerchid'],'type'=>7);
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['mid'])){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	if(isset($_RQ['is_add'])){
		$params['is_add']=intval($_RQ['is_add']);
		$return['params']['is_add']=$_RQ['is_add'];
	}
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_merch3fee_commission',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['merchname']=pdo_getcolumn('jy_merch_user',array('id'=>$item['merchid']),'merchname');
			$item['merchname']=$item['merchname']?:'无';
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'会员信息_Member','type'=>'member'),
			array('name'=>'money','title'=>'贡献值','type'=>'string'),
			array('name'=>'remark','title'=>'备注','type'=>'string'),
			array('name'=>'createtime','title'=>'时间','type'=>'time'),
		)),
		'search'=>array(
			array('name'=>'createtime','width'=>15,'title'=>'发生时间','type'=>'time_range'),
			array('name'=>'mid','width'=>15,'title'=>'会员id','type'=>'choose','action'=>'core/member.get_member/'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='export'){
	$params=array('merchid'=>$_SESSION['onemerchid'],'type'=>7);
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['mid'])){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	if(isset($_RQ['is_add'])){
		$params['is_add']=intval($_RQ['is_add']);
		$return['params']['is_add']=$_RQ['is_add'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$list=pdo_getall('jy_merch3fee_commission',$params);
	if($list){
		foreach($list as &$item){
			$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['mid']));
			$item['nickname']=$member['nickname'];
			$item['avatar']=$member['avatar'];
			if($item['is_add']==0){
				$item['fee']=-$item['fee'];
			}
			$item['op_midname']=pdo_getcolumn('core_users',array('id'=>$item['op_mid']),'name');
			$item['op_midname']=$item['op_midname']?:'无';
			$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
		}
	}
	$header=array(
		'nickname'=>'用户昵称',
		'fee'=>'贡献值变化',
		'remark'=>'备注',
		'op_midname'=>'操作员',
		'createtime'=>'时间',
	);
	cfc('tools')->export2excel($header,$list,'贡献值日志');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_merch3fee_commission',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='isqingratefee'){
	if($_RQ['id']){
		exi('请选择商户！','error');
	}
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$_RQ['id'],'credit1_fz >'=>0);
	$list=pdo_getall('jy_merch_member',$params);
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$_RQ['id']);
	foreach($list as $item){
		$params['id']=$item['id'];
		pdo_update('jy_merch_member',array('credit1_fz'=>0,'credit1 +='=>$item['credit1_fz']),$params);
	}
	exi('清空冻结成功！');
}